package study.nosql.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.eggs.result.Result;
import study.eggs.result.Results;
import study.nosql.redis.service.cache.CacheService;

/**
 * https://baijiahao.baidu.com/s?id=1730541502423010481&wfr=spider&for=pc
 * https://blog.csdn.net/qq_45637260/article/details/125866738
 * cache aside pattern
 * （1）缓存穿透
 * ======> 缓存空对象
 * ======> 布隆过滤器
 * （2）缓存击穿
 * ======> key不过期
 * ======> 互斥锁
 * （3）缓存雪崩
 * ======> key过期时间加随机值，让缓存失效时间点尽量均匀
 */
@Slf4j
@RestController
@RequestMapping("/cache")
public class _1_CacheController {

    @Autowired
    private CacheService cacheService;

    @RequestMapping("/getUserInfoFromDB")
    public Result getUserInfoFromDB(Long ubId) {
        return Results.ok(cacheService.getUserInfoFromDB(ubId));
    }

    @RequestMapping("/getUserInfo")
    public Result getUserInfo(Long ubId) {
        return Results.ok(cacheService.getUserInfo(ubId));
    }

    @RequestMapping("/get")
    public Result get(Long ubId) {
        return Results.ok(cacheService.get(ubId));
    }
}
